package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsMallService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GenVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName:temp_shop
 * author:luo
 * time:2023/2/25 14:52
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class GoodsController {
    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsMallService dtsMallService;
    /**
     * 接口地址 : http://localhost:8083/admin/goods/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "查询")
    @RequiresPermissions(value = "admin:goods:list")
    @GetMapping("/goods/list")
    public Object goodsOfList(Integer page,Integer limit,String sort,
                              String order,String goodsSn,String name){
        IPage<DtsGoods> iPage=dtsGoodsService.findGoodsList(page,limit,sort,order,goodsSn,name);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * 接口地址 : 接口地址 : http://localhost:8083/admin/goods/detail?id=23858
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "修改回显")
    @RequiresPermissions(value = "admin:goods:detail")
    @GetMapping("/goods/detail")
    public Object goodsOfDetail(Integer id){
        //1查询商品所属的二级和一级类目
        List<Integer> categoryIds=dtsGoodsService.findCategoryIdsById(id);
        //2查询商品详细信息
        DtsGoods goods=dtsGoodsService.findGoodById(id);
        //3查询商品属性
        List<DtsGoodsAttribute> attributes=dtsGoodsService.findAttributeByGoodsId(id);
        //4查询商品规格
        List<DtsGoodsSpecification> specifications=dtsGoodsService.findSpecificationsByGoodsId(id);
        //5查询商品所属库存
        List<DtsGoodsProduct> products=dtsGoodsService.findProductByGoodsId(id);
        Map data = new HashMap<>();
        data.put("categoryIds", categoryIds);
        data.put("goods", goods);
        data.put("attributes", attributes);
        data.put("specifications", specifications);
        data.put("products", products);
        return ResponseUtil.ok(data);
    }
    /**
     * 接口地址 : http://localhost:8083/admin/goods/catAndBrand
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "添加修改中品牌和分类回显")
    @RequiresPermissions(value = "admin:goods:catAndBrand")
    @GetMapping("/goods/catAndBrand")
    public Object goodsOfCatAndBrand(){
        List<CatVo> categoryList=dtsMallService.findCategoryList();
        List<GenVo> brandList=dtsGoodsService.findBrandListAll();
        Map data = new HashMap<>();
        data.put("categoryList", categoryList);
        data.put("brandList", brandList);
        return ResponseUtil.ok(data);
    }
    /**
     * 接口地址 : http://localhost:8083/admin/goods/update
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "商品修改保存")
    @RequiresPermissions(value = "admin:goods:update")
    @PostMapping("/goods/update")
    public Object goodsOfUpdate(@RequestBody GoodsAllinone goodsAllinone){
        dtsGoodsService.modifyGoods(goodsAllinone);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址 : http://localhost:8083/admin/goods/delete
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "商品删除")
    @RequiresPermissions(value = "admin:goods:delete")
    @PostMapping("/goods/delete")
    public Object goodsOfDelete(@RequestBody DtsGoods goods){
        dtsGoodsService.removeGoods(goods);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址 : http://localhost:8083/admin/goods/create
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "商品添加和上架")
    @RequiresPermissions(value = "admin:goods:create")
    @PostMapping("/goods/create")
    public Object goodsOfCreate(@RequestBody GoodsAllinone goodsAllinone){
        dtsGoodsService.addGoods(goodsAllinone);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址 : http://localhost:8083/admin/comment/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品评论"},button = "查询")
    @RequiresPermissions(value = "admin:comment:list")
    @GetMapping("/comment/list")
    public Object commentOfList(Integer page,Integer limit,String sort,
                                String order,Integer userId,Integer valueId){
        IPage<DtsComment> iPage=dtsGoodsService.findCommentList(page,limit,sort,order,userId,valueId);
        Map data=new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     *接口地址 : http://localhost:8083/admin/comment/delete
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品评论"},button = "删除")
    @RequiresPermissions(value = "admin:comment:delete")
    @PostMapping("/comment/delete")
    public Object commentOfDelete(@RequestBody DtsComment dtsComment){
        dtsGoodsService.removeComment(dtsComment);
        return ResponseUtil.ok();
    }
}
